home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 February: Tool Chest / Apple_Developer_Group_CD_Series_February_1998_Tool_Chest.iso / Sample Code / ControlBackground / ControlBackground.c < prev    next >
Encoding:
Text File  |  1997-06-19  |  2.9 KB  |  126 lines  |  [TEXT/CWIE]

  1.     //
  2.     //    You may incorporate this sample code into your
  3.     //    applications without restriction. This sample code has
  4.     //    been provided "AS IS" and the responsibility for its
  5.     //    operation is 100% yours. You are not permitted to
  6.     //    redistribute the source as "Apple sample code" after
  7.     //    having made changes. If you're going to re-distribute
  8.     //    the source, we require that you make it clear in the
  9.     //    source that the code was descended from Apple sample
  10.     //    code, but that you've made changes.
  11.     //
  12.  
  13. #define OLDROUTINELOCATIONS        0
  14. #define OLDROUTINENAMES            0
  15. #define SystemSevenOrLater        1
  16.  
  17. #ifndef __FONTS__
  18. #    include <Fonts.h>
  19. #endif
  20.  
  21. #ifndef __DIALOGS__
  22. #    include <Dialogs.h>
  23. #endif
  24.  
  25. #ifndef __QDOFFSCREEN__
  26. #    include <QDOffscreen.h> // for CTabChanged
  27. #endif
  28.  
  29. #include "MoveableModalDialog.h"
  30.  
  31. enum
  32. {
  33.     kDialogItemIndex_DrawButton = 3,
  34.     kDialogItemIndex_CheckBox
  35. };
  36.  
  37. static pascal OSErr InitMac (void)
  38. {
  39.     MaxApplZone ( );
  40.     InitGraf (&(qd.thePort));
  41.     InitFonts ( );
  42.     InitWindows ( );
  43.     InitMenus ( );
  44.     TEInit ( );
  45.     InitDialogs (nil);
  46.  
  47.     return noErr;
  48. }
  49.  
  50. static pascal OSErr Draw1ControlWithBackgroundColor (ControlRef controlRef, const RGBColor *rgb)
  51. {
  52.     //
  53.     //    This function walks the color table of the given control's
  54.     //    window looking for the content color. If it finds such a color,
  55.     //    it replaces it with the given color momentarily while drawing
  56.     //    the given control.
  57.     //
  58.  
  59.     OSErr err = noErr;
  60.  
  61.     AuxWinHandle    auxWinHandle;
  62.     WindowRef        contrlOwner                    = (**controlRef).contrlOwner;
  63.     Boolean            oldColorTableWasDefault        = GetAuxWin (contrlOwner,&auxWinHandle);
  64.     WCTabHandle        winCTabHandle                = (WCTabHandle) ((**auxWinHandle).awCTable);
  65.     short            ctIndex                        = (**winCTabHandle).ctSize;
  66.  
  67.     while (ctIndex > -1)
  68.     {
  69.         ColorSpecPtr rgbScan = ctIndex + (**winCTabHandle).ctTable;
  70.  
  71.         if (rgbScan->value == wContentColor)
  72.         {
  73.             RGBColor savedRGB = rgbScan->rgb;
  74.             rgbScan->rgb = *rgb;
  75.             CTabChanged ((CTabHandle) winCTabHandle);
  76.             Draw1Control (controlRef);
  77.             // assume memory has moved and rgbScan has become stale
  78.             (**winCTabHandle).ctTable [ctIndex].rgb = savedRGB;
  79.             CTabChanged ((CTabHandle) winCTabHandle);
  80.             return noErr; // loop never exits
  81.         }
  82.  
  83.         --ctIndex;
  84.     }
  85.  
  86.     // if the loop has exited, we didn't find the content color
  87.  
  88.     Draw1Control (controlRef);
  89.  
  90.     return err;
  91. }
  92.  
  93. void main (void)
  94. {
  95.     if (InitMac ( ))
  96.         SysBeep (10);
  97.     else
  98.     {
  99.         DialogRef dlgRef = GetNewDialog (129,nil,(WindowRef)-1);
  100.         if (dlgRef)
  101.         {
  102.             short itemHit;
  103.  
  104.             SetDialogDefaultItem (dlgRef,kStdOkItemIndex);
  105.  
  106.             do
  107.             {
  108.                 MoveableModalDialog (StdFilterProc,&itemHit);
  109.  
  110.                 if (itemHit == kDialogItemIndex_DrawButton)
  111.                 {
  112.                     short iType; Handle iHandle; Rect iRect;
  113.                     RGBColor blue = { 0,0,0xFFFF };
  114.  
  115.                     GetDialogItem (dlgRef,kDialogItemIndex_CheckBox,&iType,&iHandle,&iRect);
  116.                     if (Draw1ControlWithBackgroundColor ((ControlRef)iHandle, &blue))
  117.                         SysBeep (10);
  118.                 }
  119.             }
  120.             while (itemHit != kStdOkItemIndex);
  121.  
  122.             DisposeDialog (dlgRef);
  123.         }
  124.     }
  125. }
  126.